﻿using CQIE.JonJack.Models.Tabels;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CQIE.JonJack.Models.TabelConfig
{
    public class UserConfig : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> builder)
        {
            builder.HasKey(u => u.Id);
            builder.Property(u => u.Name).IsRequired().HasMaxLength(50);
            builder.Property(u => u.SchoolNumber).IsRequired().HasMaxLength(20);
            builder.Property(u => u.PassWord).IsRequired().HasMaxLength(100);

            
            builder.HasMany(u => u.BorrowRecords)
                   .WithOne(br => br.User)
                   .HasForeignKey(br => br.UserId);

            builder.HasMany(u => u.BookHistorys)
                   .WithOne(br => br.User)
                   .HasForeignKey(br => br.UserId);

            // 配置与Role的一对多关系
            builder.HasOne(u => u.Role)
                   .WithMany(r => r.Users)
                   .HasForeignKey(u => u.RoleId);
        }
    }
}
